var houcang_wendu = temperatureObject.rear_cabin_temperature;
var qiancang_wendu = temperatureObject.front_cabin_temperature;
var yanpan_wendu = temperatureObject.rock_temperature;
// 语言包
// 语言文本对象
const languageData = {
    "zh-tw": {
      text_1: "岩盤浴太空艙",
      text_3: "前艙",
      text_9: "後艙",
      text_13: "前艙",
      text_16: "岩盤",
   
    },
    "zh-cn": {
      text_1: "岩盘浴太空舱",
      text_3: "剩余时间",
      text_9: "后舱",
      text_13: "前舱",
      text_16: "岩盘",
  
    },
    "en-us": {
      text_1: "Stone SPA",
      text_3: "Timer",
      text_9: "Rear",
      text_13: "Front",
      text_16: "Bed",
   
    },
    "ja-jp": {
      text_1: "岩盤浴",
      text_3: "残り時間",
      text_9: "後部",
      text_13: "前部",
      text_16: "ベッド",
  
    },
  };
$('#shezhi').click(()=>{
    window.location.href = './shezhi.html'
})
$('#ad').click(()=>{
    window.location.href = './ad.html'
})
$('.to_index').click(function (){
    $('.to_index_mask').hide();
})
const isReturn = localStorage.getItem('isReturn')
if(isReturn==='1'){
    $('.to_index_mask').hide()
    localStorage.setItem('isReturn',null)
}
$('#houcang_wendu').text(houcang_wendu)
$('#houcang_up').click(()=>{
    let temperature = parseInt($('#houcang_wendu').text()) + 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度上升
        $('#houcang_wendu').text(temperature)
        $('#current_wendu_houcang').text(temperature)
        setChartsData('image_4',temperature)
    }

})
$('#houcang_down').click(()=>{
    // 后舱温度下降
    let temperature = parseInt($('#houcang_wendu').text()) - 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度下降
        $('#houcang_wendu').text(temperature)
        $('#current_wendu_houcang').text(temperature)
        setChartsData('image_4',temperature)
    }
})

$('#qiancang_wendu').text(qiancang_wendu)
$('#qiancang_up').click(()=>{
    let temperature = parseInt($('#qiancang_wendu').text()) + 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度下降
        $('#qiancang_wendu').text(temperature)
        $('#current_wendu_qiancang').text(temperature)
        setChartsData('image_5',temperature)
    }
})
$('#qiancang_down').click(()=>{
    let temperature = parseInt($('#qiancang_wendu').text()) - 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度下降
        $('#qiancang_wendu').text(temperature)
        $('#current_wendu_qiancang').text(temperature)
        setChartsData('image_5',temperature)
    }
})

$('#yanpan_wendu').text(yanpan_wendu)
$('#yanpan_up').click(()=>{

    let temperature = parseInt($('#yanpan_wendu').text()) + 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度下降
        $('#yanpan_wendu').text(temperature)
        $('#current_wendu_yanpan').text(temperature)
        setChartsData('image_6',temperature)
    }
})
$('#yanpan_down').click(()=>{
    let temperature = parseInt($('#yanpan_wendu').text()) - 5 ;
    let status = setRearCabinTemperature(temperature)
    if(status){
        //后舱温度下降
        $('#yanpan_wendu').text(temperature)
        $('#current_wendu_yanpan').text(temperature)
        setChartsData('image_6',temperature)
    }
})


// 当前温度
//     $('#current_wendu_houcang').text(current_wendu_houcang)
//     $('#current_wendu_qiancang').text(current_wendu_qiancang)
//     $('#current_wendu_yanpan').text(current_wendu_yanpan)

function initTemperature(temperatureObject) {
    let temperatureLocalObject = serialLocalStorage.get("temperatureObject")
    if (temperatureLocalObject == null) {
        $('#current_wendu_houcang').text(temperatureObject.front_cabin_temperature)
        $('#current_wendu_qiancang').text(temperatureObject.rear_cabin_temperature)
        $('#current_wendu_yanpan').text(temperatureObject.rock_temperature)
        serialLocalStorage.put("temperatureObject",temperatureObject);
        console.log("[初始化温度数据（无缓存）]:"+JSON.stringify(temperatureObject));
    } else {
        $('#current_wendu_houcang').text(temperatureLocalObject.rear_cabin_temperature)
        $('#houcang_wendu').text(temperatureLocalObject.rear_cabin_temperature)

        $('#current_wendu_qiancang').text(temperatureLocalObject.front_cabin_temperature)
        $('#qiancang_wendu').text(temperatureLocalObject.front_cabin_temperature)

        $('#current_wendu_yanpan').text(temperatureLocalObject.rock_temperature)
        $('#yanpan_wendu').text(temperatureLocalObject.rock_temperature)
        console.log("[初始化温度数据（有缓存）]:"+JSON.stringify(temperatureLocalObject));
    }

    //初始化语言格式
    
    // 从浏览器获取语言类型
    const languageType = serialLocalStorage.get("languageType")
    // 拿到语言包中的对应类型的文字
    const languageObj = languageData[languageType];
    // 拿到对应的数据类,利用返回的数组进行替换语言类型
    Object.keys(languageObj).forEach((i)=>{
        // i为对应的文字类,替换文字
        $("." + i).text(languageObj[i])
    })
    // 需要获取重定向的链接
    const openSrc = "https://mylast.video/u5z0S3y"
    try {
        fetch(openSrc, {
            method: 'GET',
            redirect: 'follow'  // 让 fetch 自动处理重定向
        })
        .then(response => {
            // 将重定向链接返回赋值给finalUrl
            // // 要解析的链接
        const finalUrl = response.url
        const videoId = new URL(finalUrl).searchParams.get("v"); // 提取 YouTube 视频 ID
        // 获取到iframe
        const iframe = document.querySelector(".video_box");
        iframe.src = `https://www.youtube.com/embed/${videoId}?autoplay=1`;
        })
    } catch (error) {
            const finalUrl = "https://www.youtube.com/watch?v=vBoP2lyAiXQ"
        const videoId = new URL(finalUrl).searchParams.get("v"); // 提取 YouTube 视频 ID
        const iframe = document.querySelector(".video_box");
        iframe.src = `https://www.youtube.com/embed/${videoId}?autoplay=1`;
    }

}
const image_options = {
    series: [
        {
            type: 'gauge',
            center: ['50%', '65%'],
            startAngle: 210,
            endAngle: -30,
            min: 0,
            max: 100,
            splitNumber: 10,
            radius:'120%',
            progress: {
                show: true,
                width: 15,
                roundCap:true,
                itemStyle: {
                    color: '#ffab91',
                },
            },
            pointer: {
                show: false
            },
            axisLine: {
                lineStyle: {
                    width: 15,
                    color:[
                        [1,'#ecceac']
                    ]
                },
                roundCap:true
            },
            splitLine: {
                show:false
            },
            axisTick:{
                show:false
            },
            axisLabel: {
                show:false
            },
            anchor: {
                show: false
            },
            title: {
                show: false
            },
            detail: {
                show:false
            },
            data: [
                {
                    value: 20
                }
            ]
        },
        {
            type: 'gauge',
            center: ['50%', '65%'],
            startAngle: 210,
            endAngle: -30,
            min: 0,
            max: 100,
            splitNumber: 10,
            radius:'120%',
            progress: {
                show: true,
                width: 5,
                roundCap:true,
                itemStyle: {
                    color: '#fd7549',
                },
            },
            pointer: {
                show: false
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            splitLine: {
                show: false
            },
            axisLabel: {
                show: false
            },
            detail: {
                show: false
            },
            data: [
                {
                    value: 20
                }
            ]
        }
    ]
};
var image_4 = echarts.init($('#image_4').get(0));
image_4.setOption(image_options);
var image_5 = echarts.init($('#image_5').get(0));
image_5.setOption(image_options);
var image_6 = echarts.init($('#image_6').get(0));
image_6.setOption(image_options);
window.addEventListener('resize', function () {
    // 调用Echarts实例的resize方法重新渲染图表
    image_4.resize();
    image_5.resize();
    image_6.resize();
});
function setChartsData(type,value){
    const newOption = {
        series: [
            {
                data:[
                    {
                        value:value
                    }
                ]
            },
            {
                data:[
                    {
                        value:value
                    }
                ]
            }
        ]
    }
    switch (type){
        case 'image_4':
            image_4.setOption(newOption)
            break;
        case 'image_5':
            image_5.setOption(newOption)
            break;
        case 'image_6':
            image_6.setOption(newOption)
            break;
    }
}
setTimeout(()=>{
    setChartsData('image_4',temperatureObject.rear_cabin_temperature)
    setChartsData('image_5',temperatureObject.front_cabin_temperature)
    setChartsData('image_6',temperatureObject.rock_temperature)
    image_4.resize();
    image_5.resize();
    image_6.resize();
},100)


function getCurrentTimeFormatted(){
    const now = new Date(); // 获取当前时间
    const year = now.getFullYear(); // 年
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月，注意要加1
    const day = String(now.getDate()).padStart(2, '0'); // 日
    const hours = String(now.getHours()).padStart(2, '0'); // 时
    const minutes = String(now.getMinutes()).padStart(2, '0'); // 分
    const seconds = String(now.getSeconds()).padStart(2, '0'); // 秒

    // 拼接成需要的格式
    $('#text_2').text(`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`)
}
setInterval(()=>{
    getCurrentTimeFormatted();
},1000)
